package com.zenika.zenbinding.client.validator;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.zenika.zenbinding.client.exception.DataMessage;
import com.zenika.zenbinding.client.model.Model;

public class ValidationContext {

	private Set models;

	public ValidationContext() {
		this.models = new HashSet();
	}

	public void addModel(Model model) {
		this.models.add(model);
	}

	/**
	 * @gwt.typeArgs <com.zenika.zenbinding.client.exception.DataMessage>
	 */
	public Set getValidationState() {
		Set result = new HashSet();
		for (Iterator iterator = this.models.iterator(); iterator.hasNext();) {
			Model currModel = (Model) iterator.next();
			DataMessage currMessage = currModel.getCurrentValidationState();
			if (currMessage != null)
				result.add(currMessage);
		}

		return result;
	}

	/**
	 * @gwt.typeArgs <com.zenika.zenbinding.client.exception.DataMessage>
	 */
	public Set validate() {
		Set result = new HashSet();
		for (Iterator iterator = this.models.iterator(); iterator.hasNext();) {
			Model currModel = (Model) iterator.next();
			DataMessage currMessage = currModel.validate();
			if (currMessage != null)
				result.add(currMessage);
		}

		return result;
	}

}
